package com.collect.tanzania.zhiScene.zhiRemind.zhiWhatsappService;

import android.annotation.SuppressLint;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiBase.BaseActivity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_WHATSAPP)
public class CTWhatsappServiceActivity extends BaseActivity {

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;
    @BindView(R.id.bar_marked)
    ZHIBottomTabBarItem bar_marked;
    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;

    private FragmentManager mFragmentManager;
    private CTWhatsappServiceFragment mCTWhatsappServiceFragment;
    private CTWhatsappAccountFragment mCTWhatsappAccountFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_whatsapp_service;
    }

    @Override
    protected void initInjector() {
//        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();
        initEvent();
        selecFragment(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ============");
    }

    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_marked.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_marked.setChecked(false);
            selecFragment(1);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_marked.setChecked(false);
                bar_account.setChecked(false);
                if (null == mCTWhatsappServiceFragment) {
                    mCTWhatsappServiceFragment = new CTWhatsappServiceFragment();
                    fragemntTransaction.add(R.id.content, mCTWhatsappServiceFragment);
                } else {
                    fragemntTransaction.show(mCTWhatsappServiceFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_marked.setChecked(false);
                bar_account.setChecked(true);
                if (null == mCTWhatsappAccountFragment) {
                    mCTWhatsappAccountFragment = new CTWhatsappAccountFragment();
                    fragemntTransaction.add(R.id.content, mCTWhatsappAccountFragment);
                } else {
                    fragemntTransaction.show(mCTWhatsappAccountFragment);
                }
            }
            default:
                break;
        }
        fragemntTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;
        if (null != mCTWhatsappServiceFragment) fragemntTransaction.hide(mCTWhatsappServiceFragment);
        if (null != mCTWhatsappAccountFragment) fragemntTransaction.hide(mCTWhatsappAccountFragment);
    }
}